<?php

namespace App\Task\Admin;


use App\Lib\Tool\Tool;
use App\Repositories\Eloquent\Admin\AdminOnlineModel;
use EasySwoole\Task\AbstractInterface\TaskInterface;

class Address implements TaskInterface
{
    public $data;
    public $adminOnlineModel;
    function __construct($data)
    {
        $this->data = $data;
        $this->adminOnlineModel = new AdminOnlineModel();
    }


    function run(int $taskId, int $workerIndex)
    {
        if($this->data['id_admin']){
             $adminOnlineObj = $this->adminOnlineModel->getItemByAdminId($this->data['id_admin']);
             list($address,$x,$y) = Tool::ipAdress($adminOnlineObj->ip);
             $adminOnlineObj->address = $address;
             $adminOnlineObj->x = $x;
             $adminOnlineObj->y = $y;
             $adminOnlineObj->update();

        }
        // TODO: Implement run() method.
    }

    function onException(\Throwable $throwable, int $taskId, int $workerIndex)
    {
        Tool::write($throwable->getMessage(),$taskId.'-'.$workerIndex.'-'.json_encode($this->data));
        // TODO: Implement onException() method.
    }


}